#ifndef OBFUSCATORENGINE_H
#define OBFUSCATORENGINE_H

#include <QObject>
#include <QString>
#include <QMap>
#include <QJsonObject>
#include <memory>

class ClassObfuscator;
class PropertyObfuscator;
class MethodObfuscator;
class TrashCodeGenerator;
class CodeValidator;
class WhitelistManager;

struct ObfuscationConfig {
    bool obfuscateClasses = true;
    bool obfuscateProperties = true;
    bool obfuscateMethods = true;
    bool injectTrashCode = true;
    bool encryptStrings = false;
    
    QString prefixPattern = "";
    QString suffixPattern = "";
    int minNameLength = 6;
    int maxNameLength = 12;
    
    QStringList excludePaths;
    QStringList includeOnly;
    
    // 白名单
    QStringList classWhitelist;     // 类名白名单
    QStringList propertyWhitelist;  // 属性白名单
    QStringList methodWhitelist;    // 方法白名单
};

struct ObfuscationResult {
    bool success = false;
    QString errorMessage;
    int classesRenamed = 0;
    int propertiesRenamed = 0;
    int methodsRenamed = 0;
    int trashCodeInjected = 0;
    QMap<QString, QString> mappings;
};

class ObfuscatorEngine : public QObject {
    Q_OBJECT
    
public:
    explicit ObfuscatorEngine(QObject *parent = nullptr);
    ~ObfuscatorEngine();
    
    // 设置项目路径
    void setProjectPath(const QString &path);
    
    // 设置配置
    void setConfig(const ObfuscationConfig &config);
    
    // 执行混淆
    ObfuscationResult obfuscate();
    
    // 验证混淆结果
    bool validate();
    
    // 回滚混淆
    bool rollback(const QString &versionId);
    
    // 获取混淆历史
    QJsonObject getHistory();
    
    // 导出映射表
    bool exportMappings(const QString &filePath);
    
signals:
    void progressUpdated(int percentage, const QString &message);
    void obfuscationStarted();
    void obfuscationCompleted(const ObfuscationResult &result);
    void errorOccurred(const QString &error);
    
private:
    QString m_projectPath;
    ObfuscationConfig m_config;
    
    std::unique_ptr<ClassObfuscator> m_classObfuscator;
    std::unique_ptr<PropertyObfuscator> m_propertyObfuscator;
    std::unique_ptr<MethodObfuscator> m_methodObfuscator;
    std::unique_ptr<TrashCodeGenerator> m_trashCodeGen;
    std::unique_ptr<CodeValidator> m_validator;
    std::unique_ptr<WhitelistManager> m_whitelist;
    QMap<QString, QString> m_lastMappings;
    
    // 扫描项目文件
    QStringList scanProjectFiles();
    
    // 备份项目
    bool backupProject();
    
    // 应用混淆映射
    bool applyMappings(const QMap<QString, QString> &mappings);
};

#endif // OBFUSCATORENGINE_H